// pages/home/index/index.js
import api from '../../../utils/api';
import util from '../../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: '航小天',
    user: '航',
    carDetail: [],
    backlogList: [], //近期待办
    recentApplication: [], //近期申请
    approvalPending: [], // 待审批
    notice: '', //最新公告
  },
  //点击头像 跳转到我的页面
  // wode(){
  //   wx.navigateTo({
  //     url: '../../mine/index/index',
  //   })
  // },
  myAgent() {
    wx.navigateTo({
      url: '../agent/agent',
    })
  },

  searchHandel() {
    wx.navigateTo({
      url: '../search/search',
    })
  },


  myApplication(e) {
    console.log(e.currentTarget.dataset.index);
    if (e.currentTarget.dataset.index == 1) {
      wx.navigateTo({
        url: '../myApplication/myApplication',
      })
    } else if (e.currentTarget.dataset.index == 0) {
      wx.navigateTo({
        url: '../myApproval/myApproval',
      })
    } else if (e.currentTarget.dataset.index == 2) {
      wx.navigateTo({
        url: '../systemAnnouncement/systemAnnouncement',
      })
    } else if (e.currentTarget.dataset.index == 3) {
      wx.navigateTo({
        url: '/pages/home/agent/agent',
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let username = wx.getStorageSync('userInfo');
    if (username) {
      this.setData({
        username: username.unitName,
        user: username.unitName.substring(0, 1),
      })
    }

    
  },
  // 基本信息
  getBasicData() {
    let that = this;
    api.getHomePageInfo({
      success: function (res) {
        // console.log(res)
        if (res.data.code == 200) {
          that.setData({
            username: res.data.data.userName,
            user: res.data.data.userName.substring(0, 1),
            carDetail: [{
                id: 0,
                num: res.data.data.examineCount,
                name: "我的审批"
              },
              {
                id: 1,
                num: res.data.data.applicationCount,
                name: "我的申请"
              },
              {
                id: 2,
                num: res.data.data.messageCount,
                name: "通知"
              },
              {
                id: 3,
                num: res.data.data.toDoCount,
                name: "我的待办"
              }
            ]
          })
          // 近期待办
          that.getBacklog()
          //近期申请
          that.getRecentApplication()
          // 待审批
          that.getApprovalPending()
          // 系统公告
          that.getNotice()
        }
      }
    })
  },
  // 系统公告
  getNotice() {
    let that = this;
    api.getMyMessage({
      data: {
        pageNum: 1,
        pageSize: 1,
        searchStr: '',
        unreadOnly: false
      },
      success: function (res) {
        // console.log(res)
        if (res.data.code == 200) {
          if (res.data.data.list.length > 0) {
            that.setData({
              notice: res.data.data.list[0]
            })
          }
        }
      }
    })
  },
  detail(e) {
    wx.navigateTo({
      url: '/pages/home/agent/agent',
    })
    // wx.navigateTo({
    //   url: '../systemDetail/systemDetail?messageId='+e.currentTarget.dataset.id,
    // })
  },
  // 近期待办
  getBacklog() {
    let that = this;
    api.getMyToDoList({
      data: {
        searchStr: '',
        pageSize: 3,
        pageNum: 1,
      },
      success: function (res) {
        if (res.data.code == 200) {
          console.log(res.data.data.list)
          for (let i = 0; i < res.data.data.list.length; i++) {
            let now = new Date(res.data.data.list[i].sendTime);
            res.data.data.list[i].sendTime = util.formatTime(now).split(' ')[0];
          }
          that.setData({
            backlogList: res.data.data.list
          })
        }
      }
    })
  },
  // 近期申请
  getRecentApplication() {
    let that = this;
    api.getMyApplication({
      data: {
        searchStr: '',
        pageSize: 3,
        pageNum: 1,
      },
      success: function (res) {
        if (res.data.code == 200) {
          // console.log(res.data.data.list)
          for (let i = 0; i < res.data.data.list.length; i++) {
            let now = new Date(res.data.data.list[i].flowTime);
            res.data.data.list[i].flowTime = util.formatTime(now).split(' ')[0];
            res.data.data.list[i].flowMoney = util.formatFigure(res.data.data.list[i].flowMoney)
          }
          that.setData({
            recentApplication: res.data.data.list
          })
        }
      }
    })
  },
  // tiaozhuan(){
  //   console.log(322)
  //   wx.navigateTo({
  //     url: '/pages/html/html?url=http://pay.gmread.cn/cl703/url.php',
  //   })
  // },
  // 待审批
  getApprovalPending() {
    let that = this;
    api.getMyExamine({
      data: {
        searchStr: '',
        pageSize: 3,
        pageNum: 1,
      },
      success: function (res) {
        if (res.data.code == 200) {
          // console.log(res.data.data.list)
          for (let i = 0; i < res.data.data.list.length; i++) {
            let now = new Date(res.data.data.list[i].flowTime);
            res.data.data.list[i].flowTime = util.formatTime(now).split(' ')[0];
            res.data.data.list[i].flowMoney = util.formatFigure(res.data.data.list[i].flowMoney)
          }
          that.setData({
            approvalPending: res.data.data.list
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //基本信息
    this.getBasicData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})